package com.example.project7;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.example.project7.utils.SharedPreferencesUtil;
import com.example.project7.viewmodel.AutoLoginState;
import com.example.project7.viewmodel.LoginViewModel;
import androidx.lifecycle.ViewModelProvider;

public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    private static final long SPLASH_DELAY = 2000; // 2秒启动页延迟
    
    private LoginViewModel loginViewModel;
    private Handler handler = new Handler();
    private Runnable navigateRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        Log.d(TAG, "onCreate");
        
        // 初始化ViewModel
        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        
        // 检查自动登录
        checkAutoLogin();
    }
    
    /**
     * 检查自动登录
     */
    private void checkAutoLogin() {
        // 观察自动登录状态
        loginViewModel.getAutoLoginState().observe(this, autoLoginState -> {
            if (autoLoginState instanceof AutoLoginState.Loading) {
                // 自动登录检查中，保持在启动页
                Log.d(TAG, "自动登录检查中...");
            } else if (autoLoginState instanceof AutoLoginState.Success) {
                // 自动登录成功，跳转到主页面
                Log.d(TAG, "自动登录成功，跳转到主页面");
                cancelNavigation();
                navigateToMain();
            } else if (autoLoginState instanceof AutoLoginState.Error) {
                // 自动登录失败，跳转到登录页面
                Log.d(TAG, "自动登录失败，跳转到登录页面");
                AutoLoginState.Error errorState = (AutoLoginState.Error) autoLoginState;
                Log.d(TAG, "自动登录失败: " + errorState.getErrorMessage());
                cancelNavigation();
                navigateToLogin();
            } else if (autoLoginState instanceof AutoLoginState.NotEnabled) {
                // 自动登录未启用，跳转到登录页面
                Log.d(TAG, "自动登录未启用，跳转到登录页面");
                cancelNavigation();
                navigateToLogin();
            }
        });
        
        // 设置定时跳转到登录页面
        navigateRunnable = () -> {
            Log.d(TAG, "启动页延迟结束，跳转到登录页面");
            navigateToLogin();
        };
        handler.postDelayed(navigateRunnable, SPLASH_DELAY);
        
        // 执行自动登录检查
        loginViewModel.autoLogin();
    }
    
    /**
     * 取消定时跳转
     */
    private void cancelNavigation() {
        if (handler != null && navigateRunnable != null) {
            handler.removeCallbacks(navigateRunnable);
        }
    }
    
    /**
     * 跳转到主页面
     */
    private void navigateToMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    
    /**
     * 跳转到登录页面
     */
    private void navigateToLogin() {
        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelNavigation();
        Log.d(TAG, "onDestroy");
    }
}